package tree_util;

import com.common.RegisterTool;
import com.common.WaterQualityDetectionTool;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.mail.imap.protocol.ID;
import db_tool.LogTool;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 10742 on 2017/7/6.
 */
public class Op_Detection extends ActionSupport {

    private String flag;
    private int detectionID;
    private String ID_WaterLayer;
    private int indexNumber;
    private String indexName;
    private String indexValue;

    public int getDetectionID() {return detectionID;}

    public void setDetectionID(int detectionID) {this.detectionID = detectionID;}

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getID_WaterLayer() {
        return ID_WaterLayer;
    }

    public void setID_WaterLayer(String ID_WaterLayer) {
        this.ID_WaterLayer = ID_WaterLayer;
    }

    public int getIndexNumber() {
        return indexNumber;
    }

    public void setIndexNumber(int indexNumber) {
        this.indexNumber = indexNumber;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public String getIndexValue() {
        return indexValue;
    }

    public void setIndexValue(String indexValue) {
        this.indexValue = indexValue;
    }

    @Override
    public String execute() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        WaterQualityDetectionTool tool = (WaterQualityDetectionTool) context.getBean("detectionTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        if (flag.equals("insert")) {
            detectionID = tool.insert(indexNumber, indexName, indexValue, ID_WaterLayer);
            System.out.println("my id is " + detectionID);
            writer.write(String.valueOf(detectionID));
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "WaterQuality_SurStatMonData", 1);
            writer.close();
        } else if (flag.equals("update")) {
            int column = tool.update(detectionID, indexNumber, indexName, indexValue);
            if (column == 1){
                writer.write("true");
                LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "WaterQuality_SurStatMonData", 1);
            }
            writer.close();
        }
        return null;
    }

    public String delete() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        WaterQualityDetectionTool tool = (WaterQualityDetectionTool) context.getBean("detectionTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        int column = tool.delete(detectionID);
        if (column == 1){
            writer.write("true");
            LogTool.insertIntoDataLog(LogTool.DELETE_CODE, "WaterQuality_SurStatMonData", 1);
        }
        writer.close();
        return null;
    }

}
